package com.woniuxy.hrp.mapper;



import com.woniuxy.hrp.entity.PayRecord;
import com.woniuxy.hrp.entity.Reservation;
import com.woniuxy.hrp.provider.ReservationSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
/**
 * desc:预约表
 * @Author: caojieke
 * @Date: 2022/8/29 12:07
 */
@Mapper
public interface ReservationMapper {
    /**
     * desc:添加预约
     * @Author: caojieke
     * @Date: 2022/8/29 12:07
     */
    @Insert("insert into hrp_reservation values(null,#{patient.id},#{doctorAndTime.id},#{status},#{payMentType.id})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
     void insert(Reservation reservation);
    /**
     * desc: 根据id查询预约
     * @Author: caojieke
     * @Date: 2022/8/29 12:10
     */
    @Select("select * from hrp_reservation where id=#{id}")
//    @Result(column = "id", property = "doctorAndTime", one = @One(select = "com.woniuxy.hrp.mapper.DoctorAndTimeMapper.selectById"))
    Reservation selectById(int id);
    /**
     * desc: 按条件查询预约
     * @Author: caojieke
     * @Date: 2022/8/29 12:08
     */
    @Results({
          @Result( column = "id",property = "id"),
            @Result( column = "patient_id",property = "patient.id"),
            @Result( column = "doctor_time_id",property = "doctorAndTime.id"),
            @Result(column = "Paymenttype_id",property = "payMentType.id")
    })
    @SelectProvider(type = ReservationSqlProvider.class, method = "getByCondition")
    List<Reservation> selectByCondition(Reservation reservation);
    /**
     * desc: 根据id更新预约状态(可以修改为任意状态)
     * @Author: caojieke
     * @Date: 2022/8/29 12:11
     */
    @Update("update hrp_reservation set status=#{status} where id=#{id} ")
     void update(Reservation reservation);


    /**
     * desc: 根据id删除预约
     * @Param: [id]
     * @Author: caojieke
     * @Date: 2022/8/29 12:12
     */
    @Delete("delete from hrp_reservation where id=#{id} ")
     void delectById(int id);

    @Update("update hrp_reservation set status=#{status} where id=#{doctorId}")
    void findByDoctorId(int doctorId);

    @Select("select * from hrp_reservation hr,hrp_doctor_time hdt where hr.doctor_time_id=hdt.id and hdt.id=#{doctorId} order by datetime asc")
    @Results(value = {
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "patient",one = @One(select = "com.woniuxy.hrp.mapper.PatientMapper.selectById")),
    })
    List<Reservation> findByIdOrderByTime(int id);

    /***
     * @description: 根据患者id查所有支付记录
     * @param: patientId
     * @return: java.util.List<com.woniuxy.hrp.mapper.PayRecordMapper>
     * @author Lrw
     * @date: 2022/8/31 0:28
     */
    @Select("select p.* from hrp_reservation r, hrp_payrecord p where p.reservation_id=r.id and r.patient_id=#{patient.id}")
    @Results(value = {
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "payrecord",one = @One(select = "com.woniuxy.hrp.mapper.PayRecordMapper.selectByReservationId")),
    })
    List<Reservation> selectPayRecordsByPatientId(int patientId);
    @Select("select * from hrp_reservation where patient_id=#{patientId} and status = #{status}")
    @Results({
            @Result( column = "patient_id",property = "patient.id"),
            @Result( column = "doctor_time_id",property = "doctorAndTime.id"),
    })
    List<Reservation> selectBypidandstatus(int patientId,String status);

    @Select("select * from hrp_reservation where status=#{status} and doctor_time_id = #{doctor_timeid}")
    @Results({
            @Result( column = "patient_id",property = "patient_id"),
            @Result( column = "doctor_time_id",property = "doctor_time_id"),
    })
    Reservation selectbypidanddtid(String status,@Param("doctor_timeid")int doctor_timeid);

    @Update("update hrp_reservation set status=#{status} , Paymenttype_id=#{payMentTypeId} where id=#{id} ")
    void update1(Reservation reservation);
    /**
     * @Author  李国杰
     * @Description //插入
     * @Date 10:28 2022/9/6
     * @Param
     * @return
     **/
    @Insert("insert into hrp_reservation(patient_id,doctor_time_id,status,Paymenttype_id,datetime,nat_id) values(#{patient.id},#{doctorAndTime.id},#{status},#{payMentType.id},#{datetime},#{nat.id})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void add(Reservation reservation);

    /**
     *@Description 根据患者id修改预约状态
     *@Author TJ
     *@Version 1.0
     **/
    @Update("update hrp_reservation set status=#{status} where id=#{id}")
    void updateByPatientId(@Param("id") int id,@Param("status") String status);
    @Delete("delete from hrp_reservation where patient_id=#{patientid} and nat_id>0 and status='n'")
    void deleNat(@Param("patientid")int patientid);


    /**
     * 查询出已付款但还未完成的预约，带出病人信息
     * @param doctorTimeId
     * @return
     */
    @Select("select * from hrp_reservation where status='y' and doctor_time_id=#{doctorTimeId}")
    @Results({
            @Result(column = "patient_id", property = "patient.id"),
            @Result(column = "patient_id", property = "patient", one = @One(select = "com.woniuxy.hrp.mapper.PatientMapper.getByid"))
    })
    List<Reservation> selectBydoctorTimeId(int doctorTimeId);

    /**
     *@Description 根据患者id查询预约状态
     *@Author TJ
     *@Version 1.0
     **/
    @Select("select * from hrp_reservation where id=#{id}")
    Reservation selectByPatientId(int id);
}
